﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAL
{
    public class BenhNhanDAL
    {
        PhongMachTuDBDataContext db;
        public List<BENHNHAN> LayDSBenhNhan()
        {
            db = new PhongMachTuDBDataContext();
            var q = from s in db.BENHNHANs
                    select s;
            return q.ToList();
        }
        public List<BENHNHAN> LayDSBenhNhan(int maBN)
        {
            db = new PhongMachTuDBDataContext();
            var q = from s in db.BENHNHANs
                    where s.MaBenhNhan==maBN
                    select s;
            return q.ToList();
        }
        public int getID()
        {

            var query = (from u in db.BENHNHANs
                         orderby u.MaBenhNhan descending
                         select u).Take(1);
            int nextID=0;
            foreach (var itemID in query)
            {
                nextID = itemID.MaBenhNhan;
            }
            return nextID;
        }
        public bool ThemBenhNhan(BENHNHAN s)
        {

            try
            {
                db = new PhongMachTuDBDataContext();
                BENHNHAN r = new BENHNHAN();
                r.HoTen = s.HoTen;
                r.GioiTinh = s.GioiTinh;
                r.NgaySinh = s.NgaySinh;
                r.DiaChi = s.DiaChi;
    
                db.BENHNHANs.InsertOnSubmit(r);
                db.SubmitChanges();
                return true;

            }
            catch (Exception)
            {

                return false;
            }
        }
        public bool suaBenhNhan(BENHNHAN s)
        {
            try
            {
                db = new PhongMachTuDBDataContext();
                BENHNHAN r = db.BENHNHANs.Single(id => id.MaBenhNhan == s.MaBenhNhan);
                r.HoTen = s.HoTen;
                r.GioiTinh = s.GioiTinh;
                r.NgaySinh = s.NgaySinh;
                r.DiaChi = s.DiaChi;
             

                db.SubmitChanges();
                return true;

            }
            catch (Exception)
            {

                return false;
            }
        }
        public bool xoaBenhNhan(int MaBenhNhan)
        {
            try
            {
                db = new PhongMachTuDBDataContext();
                var delete = from d in db.BENHNHANs
                             where d.MaBenhNhan == MaBenhNhan
                             select d;
                if (delete.Count() > 0)
                {
                    db.BENHNHANs.DeleteOnSubmit(delete.FirstOrDefault());
                    db.SubmitChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
